#include <iostream>
#include <ws2tcpip.h>
#include <winsock2.h>
#include "cmd.h"
#include "data.h"
#include "frame.h"

WSADATA wsaData;
SOCKET sockStream = INVALID_SOCKET;
struct addrinfo *result = NULL, hints, *ptr;
#define DEFAULT_IP "127.0.0.1"//服务器为本机
#define DEFAULT_PORT "9051"//服务器监听端口
#define MAX_FRAME_LEN 512


int main(int argc, char** argv) {
	int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
	if (iResult != 0) {
		std::cout << "failed" << iResult << std::endl;
		return 1;
	}
	//初始化结束
	ZeroMemory(&hints, sizeof(hints));
	hints.ai_family = AF_INET;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_protocol = IPPROTO_TCP;
	hints.ai_flags = AI_PASSIVE;

	//从本机中获取ip地址等信息为了socket使用
	iResult = getaddrinfo(DEFAULT_IP, DEFAULT_PORT, &hints, &result);
	if (iResult != 0) {
		std::cout << "getaddrinfo failed" << std::endl;
		WSACleanup();
		return 1;
	}
	//创建socket对象
	SOCKET ConnectSocket = INVALID_SOCKET;
	ConnectSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
	if (ConnectSocket == INVALID_SOCKET) {
		std::cout << "Error at socket():" << WSAGetLastError() << std::endl;
		freeaddrinfo(result);
		WSACleanup();
		return 1;
	}

	//3.连接到套接字
	for (ptr = result; ptr != NULL	; ptr = ptr->ai_next) {
		ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
		if (ConnectSocket == INVALID_SOCKET) {
			std::cout << "Error at socket():" << WSAGetLastError() << std::endl;
			freeaddrinfo(result);
			WSACleanup();
			return 1;
		}

		iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
		if (iResult == SOCKET_ERROR) {
			closesocket(ConnectSocket);
			ConnectSocket = INVALID_SOCKET;
			continue;
		}
		break;
	}
	freeaddrinfo(result);

	char recvbuf[MAX_FRAME_LEN];
	int iSendResult;
	int recvbuflen = MAX_FRAME_LEN;

	//发送客户端机器人状态
	uint16_t cmd_id = EXT_GAME_ROBOT_STATUS;
	uint16_t datasize = 0;
	switch (cmd_id) {
		case EXT_GAME_ROBOT_STATUS:
			datasize = 15;
			break;
	}
	char frame_data[MAX_FRAME_LEN];
	memset(frame_data, 0, MAX_FRAME_LEN);
	framehdr* frame_header;
	ext_game_robot_status_t *data;
	//填充头部
	frame_header = (framehdr*)frame_data;
	frame_header->daga_length = datasize;
	frame_header->seq = 1;
	//填充cmd命令
	*(frame_data + sizeof(framehdr)) = cmd_id;
	//填充数据段
	data = (ext_game_robot_status_t*)(frame_data + sizeof(framehdr) + 2);
	data->robot_id = 1;
	data->robot_level = 1;
	data->remain_HP = 0;
	//发送
	iSendResult = send(ConnectSocket, frame_data, 7 + datasize, 0);
	if (iSendResult == SOCKET_ERROR) {
		std::cout << "send failed:" << WSAGetLastError() << std::endl;
		closesocket(ConnectSocket);
		WSACleanup();
		return 1;
	}

	iResult = shutdown(ConnectSocket, SD_SEND);
	if (iResult == SOCKET_ERROR) {
		std::cout << "shutdown failed:" << WSAGetLastError() << std::endl;
		closesocket(ConnectSocket);
		WSACleanup();
		return 1;
	}

	do {
		iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
		if (iResult > 0) {
			//收到比赛结果通知
			framehdr* frame_header;
			uint16_t datalen;
			frame_header = (framehdr*)recvbuf;
			if (frame_header->SOF != 0xA5) {
				std::cout << "不是比赛协议" << std::endl;
				return 1;
			}
			datalen = frame_header->daga_length;
			ext_game_result_t* data;
			data = (ext_game_result_t*)(recvbuf + sizeof(framehdr) + sizeof(cmd_id));
			if (data->winner == 0) {
				std::cout << "平局" << std::endl;
			} else if (data->winner == 1) {
				std::cout << "胜利" << std::endl;
			} else {
				std::cout << "对抗失败" << std::endl;
			}
		} else if (iResult == 0)
			std::cout << "Connection closed....." << std::endl;
		else
			std::cout << "recv failed:" << WSAGetLastError() << std::endl;
	} while (iResult > 0);
	closesocket(ConnectSocket);
	WSACleanup();
	
	
	char* test;
	uint16_t si;
	fill_EXT_GAME_STATUS_data(test,si);
	
	


	return 0;
}
